﻿using System;
using Xenta.Entities;
using Xenta.Enums;
using Xenta.Utils;

namespace Xenta.Operations
{
    /// <summary>
    /// Creates a sales rule.
    /// </summary>
    public class CreateVolumeDiscountRule : CreateSalesRule<VolumeDiscountRuleEntity>
    {
        #region Properties

        /// <summary>
        /// Gets or sets the lower threshold.
        /// </summary>
        public decimal ThresholdLower
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the upper threshold.
        /// </summary>
        public decimal ThresholdUpper
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the amount.
        /// </summary>
        public decimal Amount
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public VolumeDiscountRuleFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override Err Validate(IUnitOfWork uow)
        {
            if(Amount <= Decimal.Zero)
                return Err.Of<VolumeDiscountRuleEntity>().NotValid("Amount");
            if(ThresholdUpper != Decimal.Zero && ThresholdUpper < ThresholdLower)
                return Err.Of<VolumeDiscountRuleEntity>().NotValid("Threshold");
            return base.Validate(uow);
        }

        protected override void Construct(IUnitOfWork uow, VolumeDiscountRuleEntity entity)
        {
            base.Construct(uow, entity);

            entity.ThresholdLower = ThresholdLower;
            entity.ThresholdUpper = ThresholdUpper;
            entity.Amount = Amount;
            entity.Flags = Flags;
        }

        #endregion
    }

    /// <summary>
    /// Updates the sales rule.
    /// </summary>
    public class UpdateVolumeDiscountRule : UpdateSalesRule<VolumeDiscountRuleEntity>
    {
        #region Properties

        /// <summary>
        /// Gets or sets the lower threshold.
        /// </summary>
        public decimal ThresholdLower
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the upper threshold.
        /// </summary>
        public decimal ThresholdUpper
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the amount.
        /// </summary>
        public decimal Amount
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public VolumeDiscountRuleFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override Err Validate(IUnitOfWork uow)
        {
            if(Amount <= Decimal.Zero)
                return Err.Of<VolumeDiscountRuleEntity>().NotValid("Amount");
            if(ThresholdUpper != Decimal.Zero && ThresholdUpper < ThresholdLower)
                return Err.Of<VolumeDiscountRuleEntity>().NotValid("Threshold");
            return base.Validate(uow);
        }

        protected override void Revise(IUnitOfWork uow, VolumeDiscountRuleEntity entity)
        {
            base.Revise(uow, entity);

            entity.ThresholdLower = ThresholdLower;
            entity.ThresholdUpper = ThresholdUpper;
            entity.Amount = Amount;
            entity.Flags = Flags;
        }

        #endregion
    }
}
